package com.jxc.biz.impl;

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.jxc.biz.InquiryBiz;
import com.jxc.dao.InquiryDao;
import com.jxc.dao.RecordDao;
import com.jxc.entity.Inquiry;
import com.jxc.entity.Inquirylist;
import com.jxc.entity.Record;
import com.jxc.entity.Recordlist;
import com.jxc.util.PageBean;

public class InquiryBizImpl implements InquiryBiz {
	private InquiryDao dao;
	private RecordDao recordDao;

	public void deleteInquiry(Inquiry inquiry) {

		dao.deleteInquiry(inquiry);
	}
	public Inquiry findInquiry(Inquiry inquiry) {
		return dao.findInquiry(inquiry);
	}
	public PageBean findInquiryList(String hql, PageBean bean) {
		String sql="from Record record where record.type=1 and record.state=2";
		recordDao.getRecordList(sql, bean);
	  return dao.findInquiryList(hql, bean);
	}
	public void updateRedord(Inquiry inquiry){
		Record record=recordDao.getRecordBySn(inquiry.getSn());
		record.setEnddate(new Date());
		record.setStaffId(inquiry.getStaff().getId());
		record.setTotalprice(inquiry.getTotalprice());
		recordDao.update(record);
	
	}
	public void insertInquiry(Inquiry inquiry) {
		dao.insertInquiry(inquiry);
		if(inquiry.getState().equals("2")){
			updateRedord(inquiry);
			return;
		}
		Record record=new Record();
		record.setSn(inquiry.getSn());
		record.setFlowId(inquiry.getId());
		record.setUserId(inquiry.getUser().getId());
		record.setType("1");
		record.setState(inquiry.getState());
		record.setCreatedate(new Date());
		Set<Recordlist> rlist=new HashSet<Recordlist>();
	    Iterator<Inquirylist> it=inquiry.getInquirylists().iterator();
	    while(it.hasNext()){
	    	Inquirylist _in=it.next();
		   Recordlist recordlist=new Recordlist();
		   recordlist.setCommoditySn(_in.getCommodity().getSn());
		   recordlist.setCompanyname(inquiry.getUser().getEnterprise().getName());
		   recordlist.setNumber(_in.getNumber());
		   recordlist.setUsername(inquiry.getUser().getName());
		   recordlist.setPrice(_in.getCommodity().getSupplyprice());
		   recordlist.setRecord(record);
		   rlist.add(recordlist);
	    }
		record.setRecordlists(rlist);
		recordDao.insertRecord(record);
	}
	public void updateInquiry(Inquiry inquiry) {
		dao.updateInquiry(inquiry);
	}
	public InquiryDao getDao() {
		return dao;
	}
	public void setDao(InquiryDao dao) {
		this.dao = dao;
	}
	public Inquiry findInquiryById(int inquiryId) {
		
		return dao.findInquiryById(inquiryId);
	}
	
	public RecordDao getRecordDao() {
		return recordDao;
	}
	public void setRecordDao(RecordDao recordDao) {
		this.recordDao = recordDao;
	}
}
